<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>变亮</title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				width: 100vw;
				height: 100vh;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			
			canvas {
				width: 500px;
				height: 500px;
				border: 2px solid blue;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<canvas id="canvas" width="500" height="500"></canvas>
			<input type="number" id="nu-light"/>
			<input type="button" name="light" id="light" value="变亮" />
		</div>
	</body>
	<script type="text/javascript">
		window.onload = function() {
			let oCanvas = document.getElementById('canvas');
			console.dir(oCanvas);
			let oPen = oCanvas.getContext("2d");
			var image = new Image();
			image.src = 'img.png'
			
			image.onload = function() {
				oPen.drawImage(image, 0, 0, 500, 500);

				//开始坐标  获取像素的宽和高
				//getImageData(x,y,width,height)
				var imageData = oPen.getImageData(0, 0, 500, 500)
				var data = imageData.data
				console.dir(data)

				document.getElementById('light').onclick = function() {

					let light = parseInt(document.getElementById('nu-light').value)
					
					console.dir(light)
					for(var i = 0; i < data.length; i += 4) {
						data[i + 0] += light
						data[i + 1] += light
						data[i + 2] += light
					}
					//渲染像素
					//putImageData(imageData,x,y)
					oPen.putImageData(imageData, 0, 0)
				}
			}
		}
	</script>
	</body>

</html>